package ui 
{
	import Base.Loader.SWFLoader;
	
	import Common.GameDefine;
	import Common.RenderManager;
	
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import net.NpcNetHandler;

	/**
	 * ...
	 * @author ...
	 */
	public class NpcTalkUIHandler extends uiBase
	{
		public var id:int = GameDefine.INVALIDID;
		public var type:int = GameDefine.INVALIDID;
		public var mode:int = GameDefine.INVALIDID;
		
		public var title:String = "";
		public var talk:String = "";
		
		public const BUTTON_MODE_0:int = 0;
		public const BUTTON_MODE_1:int = 1;
		public const BUTTON_MODE_2:int = 2;
		public const BUTTON_MODE_3:int = 3;
		
		public const NS_ALREADY:int = 0;
		public const NS_DENIED:int = 1;
		
		
		public const EVENT_SELECT:String = "select";
		
		public function NpcTalkUIHandler() 
		{
			path = "npc.swf";
			name = "NpcTalkUI";
		}
		
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = RenderManager.instance.stageWidth * 0.5;
				uiMc.y = RenderManager.instance.stageHeight * 0.5;
			}
		}
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			uiMc.setText( title , talk );
			uiMc.setText( "build 0.5，" , "更新角色动作加载方式，改用石器原有的调色板，，\n出村野外可以打怪，掉肉和道具，组队时不要逃跑会有问题。\n" + "<a href='event:select:0' ><u>aaaa</u></a>\n" + "<a href='http://www.baidu.com' target='_blank' >www.baidu.com</a>" );
			uiMc.setButtonMode( BUTTON_MODE_1 );
			
			uiMc.okButton.addEventListener( MouseEvent.CLICK , onOk );
			uiMc.cancelButton.addEventListener( MouseEvent.CLICK , onCancel );
			uiMc.closeButton.addEventListener( MouseEvent.CLICK , onClose );
			uiMc.wordText.addEventListener( TextEvent.LINK , onTextLink );
		}
		
		public function onTextLink( event:TextEvent ):void
		{
			var a:Array = event.text.split( ":" );
			var type:String = a[ 0 ];
			
			switch ( type )
			{
				case EVENT_SELECT:
				{
					NpcNetHandler.sendTalkNpc( id , GameDefine.INVALIDID , a[1] );
					uiMc.visible = false;
					
					var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
					
					if ( this.type == GameNPC.HEALER )
					{
						mainPlayer.heal( a[1] );
					}
					
					RoleInfoUIHandler.instance.updateData();
					PetUIHandler.instance.updateData();
				}
				break;
			}
		}
		
		public function onOk( event:MouseEvent ):void
		{
			NpcNetHandler.sendTalkNpc( id , NS_ALREADY , GameDefine.INVALIDID );
			
			uiMc.visible = false;
		}
		
		public function onCancel( event:MouseEvent ):void
		{
			NpcNetHandler.sendTalkNpc( id , NS_DENIED , GameDefine.INVALIDID );
			
			uiMc.visible = false;
		}
		
		public function onClose( event:MouseEvent ):void
		{
			uiMc.visible = false;
		}
		
		public function setText():void
		{
			if ( uiMc )
			{
				uiMc.setButtonMode( mode );
				uiMc.setText( title , talk );
			}
		}
		
		public static var instance:NpcTalkUIHandler = null;
		public static function Instance():NpcTalkUIHandler
		{
			if ( instance == null )
			{
				instance = new NpcTalkUIHandler();
			}
			
			return instance;
		}
	}

}